package com.topscomm.mis.cbb.pm.service.abs;

import cn.hutool.core.thread.ThreadUtil;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mis.cbb.pm.api.IFlowSkipService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;


@ConditionalOnMissingBean(IFlowSkipService.class)
public abstract class AbstractFlowSkipService implements IFlowSkipService {
    @Resource(name = "cbbThreadPool")
    private ExecutorService cbbThreadPool;
    private static final Logger logger = LoggerFactory.getLogger(IFlowSkipService.class);
    @Override
    public void testThread() {
        try {
            List<Future> futureTaskList = new ArrayList<Future>();

            // 查找生产订单及入库单
            futureTaskList.add(cbbThreadPool.submit(() -> {
                logger.info("start service");
                try{
                    Thread.sleep(5000);
                }catch(Exception e){
                    e.printStackTrace();
                }
                logger.info("end service");
                    }

            ));

            for (int i = 0; i < futureTaskList.size() - 1; i++) {
                //阻塞所有线程执行完毕
                futureTaskList.get(i).get();
                System.out.println(i);
            }
            System.out.println(2234);
        } catch (InterruptedException e){
            logger.error("Exception:{}",e.getMessage());
            Thread.currentThread().interrupt();
        } catch (Exception e) {
            logger.error("多线程导出过程出现异常:{}", e.getMessage());
            throw new ServiceException("导出过程出现异常，请联系管理员处理！");
        }

    }
}
